package cl.usm.acceso.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import cl.usm.acceso.bean.RegistraBean;
import cl.usm.acceso.bean.ServicioBean;

public class RegistraDAO {

	private int idPrenda;
	private int idServicio;
	
	public int getIdPrenda() {
		return idPrenda;
	}
	
	public void setIdPrenda(int idPrenda) {
		this.idPrenda = idPrenda;
	}
	
	public int getIdServicio() {
		return idServicio;
	}
	
	public void setIdServicio(int idServicio) {
		this.idServicio = idServicio;
	}

	public List<RegistraBean> getListaRegistra(int idPrenda) {
		
		List<RegistraBean> list = new ArrayList<RegistraBean>();
		
		try {
			InitialContext context = new InitialContext();
			DataSource dataSource = (DataSource) context.lookup("jdbc/Lavasoft");
			
			Connection connection = dataSource.getConnection();
			Statement statement = connection.createStatement();
			String sql = "Select idPrenda, idServicio from registra where idPrenda = '" + idPrenda + "'";
	
			ResultSet resultSet = statement.executeQuery(sql);
	
			while (resultSet.next()) {
				RegistraBean item = new RegistraBean();
				item.setIdPrenda(resultSet.getInt("idPrenda"));
				item.setIdServicio(resultSet.getInt("idServicio"));
				list.add(item);
			}
	
			resultSet.close();
			statement.close();
			connection.close();
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public int seleccionar(int idPrenda) {
		int result = -1;
		try {
			InitialContext context = new InitialContext();
			DataSource dataSource = (DataSource) context.lookup("jdbc/Lavasoft");
			
			Connection connection = dataSource.getConnection();
			Statement statement = connection.createStatement();
			String sql = "Select idServicio from registra where idPrenda = '" + idPrenda + "'";
	
			ResultSet resultSet = statement.executeQuery(sql);
			
			while (resultSet.next()) {
				setIdServicio(resultSet.getInt("idServicio"));
				result = 1;
			}
	
			resultSet.close();
			statement.close();
			connection.close();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public int agregar(int idPrenda, int idServicio) {
		int result = -1;
		try {
			InitialContext context = new InitialContext();
			DataSource dataSource = (DataSource) context.lookup("jdbc/Lavasoft");
			
			Connection connection = dataSource.getConnection();
			Statement statement = connection.createStatement();
			String sql = "Insert into registra (idPrenda, idServicio) values ('"
					+ idPrenda + "','" + idServicio + "')";
	
			result = statement.executeUpdate(sql);
	
			statement.close();
			connection.close();
			result = 1;
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public int modificar(int idPrenda, int idServicio) {
		int result = -1;
		try {
			InitialContext context = new InitialContext();
			DataSource dataSource = (DataSource) context.lookup("jdbc/Lavasoft");
			
			Connection connection = dataSource.getConnection();
			Statement statement = connection.createStatement();
		    
			String sql = "update registra set idServicio = '" + idServicio
					+ "' where idPrenda = '" + idPrenda + "'";
			result = statement.executeUpdate(sql);
	
			statement.close();
			connection.close();
			result = 1;
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public int eliminar(int idPrenda) {
		int result = -1;
		try {
			InitialContext context = new InitialContext();
			DataSource dataSource = (DataSource) context.lookup("jdbc/Lavasoft");
			
			Connection connection = dataSource.getConnection();
			Statement statement = connection.createStatement();
		    
			String sql = "delete from registra where idPrenda = '" + idPrenda + "'";
			result = statement.executeUpdate(sql);
	
			statement.close();
			connection.close();
			result = 1;
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;		
	}
}
